<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Suser extends SHOPS_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->model('suser_mdl');
    }

    /**
     * 用户操作
     * 只对管理员权限开放
     */
    public function index(){
        
    }

    /**
     * 修改密码
     */
    public function reset_password(){
        if($this->input->is_ajax_request()){
            //用户修改密码
            $post = $this->input->post(NULL,TRUE);
            if($post['password1'] != $post['password2']){
                die('两次密码输入不一致');
            }

            if(!preg_match("/^[0-9a-zA-Z]{5,20}$/",ltrim($post['password2']))){
                echo '密码的长度为6-20位，不能为空';
                exit;
            }

            $this->db->where('id',self::$suser['id']);
            $res = $this->suser_mdl->get_one();

            if(empty($res)){
                die('原始密码错误！');
            }

            $this->db->where('id',self::$suser['id']);
            $res = $this->suser_mdl->update([
                'password' => md5($post['password1'])
            ]);
            //清空session
            $this->session->unset_userdata('suser');
            echo $res === true ? 1 : '修改密码失败！';
        }else{
            $this->_view('suser_restpwd');
        }
    }

    /**
     * 用户退出
     */
    public function quit(){
        $this->session->unset_userdata('suser');
        redirect(site_url('shops/login/index'));
    }

}

/* End of file Suser.php */
/* Location: ./application/controllers/shops/Suser.php */